package com.yuanli.cosmeticmirror.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.model.iview.IRegisterView;
import com.yuanli.cosmeticmirror.persenter.RegisterPersenter;
import com.yuanli.cosmeticmirror.utils.GeneralUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册
 *
 * @author undeswes
 */
public class RegisterActivity extends BaseActivity implements IRegisterView {
    @BindView(R.id.register_account)
    EditText registerAccount;
    @BindView(R.id.register_code)
    EditText registerCode;
    @BindView(R.id.register_get_code)
    TextView registerGetCode;
    @BindView(R.id.register_password)
    EditText registerPassword;
    @BindView(R.id.register_enter)
    TextView registerEnter;
    @BindView(R.id.register_to_login)
    TextView registerToLogin;
    private RegisterPersenter persenter;
    private static int tempSecond = 60; // 重新发送短信时间
    private long mLastClickTime = 0;
    public final long TIME_INTERVAL = 1000L;
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    registerGetCode.setText("重新发送(" + tempSecond-- + "s)");
                    if (tempSecond > 0) {
                        handler.sendEmptyMessageDelayed(1, 1000); // 重新发送ns秒
                    } else {
                        handler.sendEmptyMessage(2); // 重新获取验证码
                    }
                    break;
                case 2:
                    registerGetCode.setText("获取验证码");
                    //      mBtnGetCode.setTextColor(getResources().getColor(R.color.c_222));
                    registerGetCode.setClickable(true);
                    tempSecond = 60;
                    break;
                case 100:
                    break;
            }
        }
    };
    private boolean isSucess = false;

    @Override
    protected int getContentView() {
        return R.layout.activity_register;
    }

    @Override
    protected void init(Bundle bundle) {
        persenter = new RegisterPersenter(this, this);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void destroy() {
        handler.removeCallbacksAndMessages(null);
        handler = null;
    }


    @OnClick({R.id.register_get_code, R.id.register_enter, R.id.register_to_login})
    public void onViewClicked(View view) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - mLastClickTime < TIME_INTERVAL) {
            // do something
            return;
        }
        mLastClickTime = nowTime;
        switch (view.getId()) {
            case R.id.register_get_code:
                String phone = registerAccount.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    showToast("请输入手机号");
                    return;
                }
                if (GeneralUtils.isNotTel(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                tempSecond = 60;
                handler.sendEmptyMessage(1); // 重新发送ns秒
                registerGetCode.setClickable(false); // 取消点击事件1
                persenter.getCode(phone);
                break;
            case R.id.register_enter:
                String name = registerAccount.getText().toString();
                String password = registerPassword.getText().toString();
                if (!isSucess) {
                    showToast("请先获取验证码再进行注册");
                    return;
                }
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
                    showToast("请将信息输入完整再进行注册");
                    return;
                }
                if (password.length() < 6) {
                    showToast("密码不能少于6位");
                    return;
                }
                showLoadDialog("正在注册..");
                persenter.register(name, name, password);
                break;
            case R.id.register_to_login:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void getCodeSuncess(String code) {
        isSucess = true;
        //        registerCode.setText(code);
        showToast("获取验证码成功");
        registerCode.setFocusable(true);
        registerCode.setFocusableInTouchMode(true);

    }

    @Override
    public void Error(int code, String msg) {
        dialogDismiss();
        if (code == 1) {
            isSucess = false;
        }
        showToast(msg);
    }

    @Override
    public void Sucess() {
        dialogDismiss();
        showToast("注册成功");
        startActivity(new Intent(this, LoginActivity.class).putExtra("userName", !TextUtils.isEmpty(registerAccount.getText().toString()) ? registerAccount.getText().toString() : ""));
        finish();
    }
}
